package com.nodecollege.cloud.service.impl.deal;

import com.alibaba.fastjson.JSON;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.po.ChatGroupUser;
import com.nodecollege.cloud.common.model.vo.ChatGroupUserData;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.ChatService;
import com.nodecollege.cloud.service.GroupUserService;
import com.nodecollege.cloud.service.impl.ChatServiceAbstrat;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 群用户信息修改
 *
 * @author LC
 * @date 2020/2/26 23:07
 */
@Slf4j
@Service
public class ChatUpdateGroupUserServiceImpl extends ChatServiceAbstrat implements ChatService {

    @Autowired
    private GroupUserService groupUserService;

    /**
     * 获取群组用户信息
     *
     * @param chatData
     * @param channel
     */
    @Override
    public void handler(String chatData, Channel channel) {
        log.info("ChatUpdateGroupUserServiceImpl handler");
        ChatGroupUserData groupData = JSON.parseObject(chatData, ChatGroupUserData.class);
        Long userId = ThreadUserUtils.getLoginUser().getUserId();
        ChatGroupUser groupUser = new ChatGroupUser();
        groupUser.setUserId(userId);
        groupUser.setGroupId(groupData.getGroupId());
        groupUser.setNickname(groupData.getNickname());
        groupUser.setGroupState(groupData.getGroupState());
        groupUser = groupUserService.updateGroupUser(groupUser);
        // 给群所有人发送发送
        sendUpdateGroupUser(groupUser);
    }

    /**
     * 向群组所有人发送群用户更新数据
     *
     * @param updateUser
     */
    public void sendUpdateGroupUser(ChatGroupUser updateUser) {
        sendGroupMsg(updateUser.getGroupId(), NCResult.ok(updateUser), 8);
    }
}
